package com.xhapi.utils.ocrUtils.xhOCR;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import com.xhapi.config.XfXhConfig;
import com.xhapi.utils.FileUtil;
import com.xhapi.utils.HttpUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class HandWrite {
    private final XfXhConfig xfXhConfig;
    // 手写文字识别webapi接口地址
    public static final String WEBOCR_URL = "https://webapi.xfyun.cn/v1/service/v1/ocr/handwriting";

    /**
     * 组装http请求头
     *
     * @param aue
     * @param engineType
     * @return
     * @throws UnsupportedEncodingException
     * @throws ParseException
     */
    public Map<String, String> constructHeader(String language, String location) throws UnsupportedEncodingException, ParseException {
        // 系统当前时间戳
        String X_CurTime = System.currentTimeMillis() / 1000L + "";
        // 业务参数
        String param = "{\"language\":\""+language+"\""+",\"location\":\"" + location + "\"}";
        String X_Param = new String(Base64.encodeBase64(param.getBytes("UTF-8")));
        // 接口密钥
        String apiKey = "54267c50f7f545669a1d4cf08bc526d9";//xfXhConfig.getApiKey();
        // 讯飞开放平台应用ID
        String X_Appid =xfXhConfig.getAppId();
        // 生成令牌
        String X_CheckSum = DigestUtils.md5Hex(apiKey + X_CurTime + X_Param);
        // 组装请求头
        Map<String, String> header = new HashMap<String, String>();
        header.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        header.put("X-Param", X_Param);
        header.put("X-CurTime", X_CurTime);
        header.put("X-CheckSum", X_CheckSum);
        header.put("X-Appid", X_Appid);
        return header;
    }

    public static void main(String[] args) throws IOException,ParseException{

    }
}
